#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "yzjreader.h"


#include <QString>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
#include <QStatusBar>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 填充tableWidget
    //connect(ui->actionMonitor,SIGNAL(triggered()),qApp,SLOT(cpu_monitor()));
//    QTimer *timer = new QTimer(this);
//    connect(timer,SIGNAL(timeout()),this,SLOT(cpu_monitor()));
//    timer->start(2000);
//    connect(ui-,SIGNAL(triggered(QAction*)),this,SLOT(showShow(QAction*)));


//    oReader.SetTreeWidget(ui->twServerMonitor);
//    oReader.start();
    // 现在的问题依然聚焦在reader上了
//    ReadThread oReader(ui->twServerMonitor);
//    oReader.start();

    file.setFileName("record.yzj");
    if(!file.open(QFile::ReadOnly)){
        QMessageBox msg;
        msg.setText("CAN OPEN record.yzj");
        msg.exec();
    }

    connect(&oReader, SIGNAL(StartRead()), this, SLOT(StartRead())); //y
    connect(&oReader, SIGNAL(StartCompute()), this, SLOT(StartCompute()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

//void MainWindow::cpu_monitor()
//{
//    // 这些数据就来源于业务处理类
//    int cpuCounts = 1;
//    QString cpuElements[4] = {"1.2", "3.4", "5.6", "7.8"};
//
//    int columeCount = ui->twCPU->columnCount();
//    int rowCount = cpuCounts;
//    for(int i = 0; i< columeCount; i++)
//    {
//        ui->twCPU->setItem(rowCount - 1, i, new QTableWidgetItem(cpuElements[i]));
//    }
//}

void MainWindow::StartRead(){

    YzjReader yReader(ui->twServerMonitor);
    yReader.ReadYzj(&file);
}

void MainWindow::on_actionShow_triggered()
{
    pShow = new Show;
    pShow->show();
    pShow->activateWindow();
}

void MainWindow::on_actionMonitor_triggered()
{
    // LET ME HAVE A TEST
    pMonitor = new Monitor;
    pMonitor->show();
    pMonitor->activateWindow();

}

void MainWindow::open(){
    QString fileName =
            QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
                                         QDir::currentPath(),
                                         tr("YZJ Files (*.yzj *.xml)"));
    if (fileName.isEmpty())
        return;
//    QString fileName = "record.yzj";// 要打开文件名
    ui->twServerMonitor->clear();   // 清空tw

    QFile file(fileName);
    if(!file.open(QFile::ReadOnly | QFile::Text)){
        QMessageBox::warning(this, QObject::tr("Open File:")
                             , QObject::tr("Cannot read file %1:\n%2").
                             arg(fileName).
                             arg(file.errorString()));
        return;
    }

    YzjReader reader(ui->twServerMonitor);

    if (!reader.ReadYzj(&file)) {
            QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
                                 tr("Parse error in file %1:\n\n%2")
                                 .arg(fileName)
                                 .arg(reader.errorString()));
        }else{
            statusBar()->showMessage(QObject::tr("File loaded"),2000);
        }
}

//
void MainWindow::start(){
    oWriter.start();
    // 依然没有显示，可能是新生产的QTreeWidget?
    oReader.start();

//    // 写入文件, 文件中始终只有一个记录，再次写入时将原来的内容删除
//
//    WriteThread oWriter;
//    oWriter.start();
//
//    // 提取一条记录显示在tw中
//    ReadThread oReader(ui->twServerMonitor);
//    oReader.start();
//
////    oWriter.wait();
////    oReader.wait();
}
